﻿using Liukun.SmartParking.Client.Models;
using Prism.Commands;
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using Zhaoxi.SmartParking.Client.BLL;
using Zhaoxi.SmartParking.Client.IBLL;

namespace Liukun.SmartParking.Client.ViewModels
{
    public class WinLoginViewModel : BindableBase
    {
        public UserModel UserInfo { get; set; } = new UserModel();
        public DelegateCommand<object> DoLoginCmd { get; set; }
        ILoginBLL _loginBLL;
        public WinLoginViewModel(ILoginBLL loginBLL)
        {
            _loginBLL = loginBLL;
            DoLoginCmd = new DelegateCommand<object>(DoLogin);
        }

        private void DoLogin(object obj)
        {
            this.ErrorMessage = "";
            if (string.IsNullOrEmpty(UserInfo.UserName))
            {
                this.ErrorMessage = "请输入用户名";
                return;
            }
            if (string.IsNullOrEmpty(UserInfo.Password))
            {
                this.ErrorMessage = "请输入密码";
                return;
            }
            // 打开Loading动画 
            this.ErrorMessage = "正在登录....";
            this.IsLoading = true; 
            (obj as Window).DialogResult = true;// 关闭登录   进入主窗口
            //Task.Delay(3000).Wait();
            //Task.Run(async () =>
            //{
            //    await Task.Delay(3000);
            //    try
            //    {
            //        var user = await _loginBLL.Login(UserInfo.UserName, UserInfo.Password);
            //        if (user != null)
            //        {
            //            // 将用户信息保存在内存中
            //            // 对接 
            //            // 登录成功   请求Api    耗时操作    Loading动画显示
            //            (obj as Window).DialogResult = true;// 关闭登录   进入主窗口
            //        }
            //        else
            //        {
            //            throw new Exception("登录失败，未知错误");
            //        }
            //    }
            //    catch (Exception ex)
            //    {
            //        // 异常信息  在界面显示
            //        ErrorMessage = ex.Message;
            //    }
            //    finally
            //    {
            //        // 关闭Loading动画
            //        this.IsLoading = false;
            //    }
            //});
        }

        /// <summary>
        /// 是否正在登录
        /// </summary>
        private bool _IsLoading = false;

        public bool IsLoading
        {
            get { return _IsLoading; }
            set { SetProperty(ref _IsLoading, value); }
        }

        /// <summary>
        /// 登录情况
        /// </summary>
        private string _ErrorMessage;

        public string ErrorMessage
        {
            get { return _ErrorMessage; }
            set { SetProperty(ref _ErrorMessage, value); }
        }
        private string _LoadMessage;

        public string LoadMessage
        {
            get { return _LoadMessage; }
            set { SetProperty(ref _LoadMessage, value); }
        }

    }
}
